package com.ruoyi.project.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.product.domain.InsProject;
import com.ruoyi.project.product.service.IInsProjectService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 产品Controller
 *
 * @author taney
 * @date 2020-11-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/product/project")
public class InsProjectController extends BaseController {

    private final IInsProjectService iInsProjectService;

    /**
     * 查询产品列表
     */
    @PreAuthorize("@ss.hasPermi('product:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsProject insProject) {
        startPage();
        LambdaQueryWrapper<InsProject> lqw = new LambdaQueryWrapper<InsProject>();
        if (StringUtils.isNotBlank(insProject.getName())) {
            lqw.like(InsProject::getName, insProject.getName());
        }
        if (StringUtils.isNotBlank(insProject.getNameDesc())) {
            lqw.eq(InsProject::getNameDesc, insProject.getNameDesc());
        }
        if (StringUtils.isNotBlank(insProject.getShortName())) {
            lqw.like(InsProject::getShortName, insProject.getShortName());
        }
        if (StringUtils.isNotBlank(insProject.getItemShortName())) {
            lqw.like(InsProject::getItemShortName, insProject.getItemShortName());
        }
        if (insProject.getCodeNo() != null) {
            lqw.like(InsProject::getCodeNo, insProject.getCodeNo());
        }
        if (StringUtils.isNotBlank(insProject.getEffectivePeriod())) {
            lqw.eq(InsProject::getEffectivePeriod, insProject.getEffectivePeriod());
        }
        if (StringUtils.isNotBlank(insProject.getCreator())) {
            lqw.eq(InsProject::getCreator, insProject.getCreator());
        }
        if (insProject.getCreatetime() != null) {
            lqw.eq(InsProject::getCreatetime, insProject.getCreatetime());
        }
        if (StringUtils.isNotBlank(insProject.getTestMethod())) {
            lqw.eq(InsProject::getTestMethod, insProject.getTestMethod());
        }
        List<InsProject> list = iInsProjectService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @PreAuthorize("@ss.hasPermi('product:project:export')")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsProject insProject) {
        LambdaQueryWrapper<InsProject> lqw = new LambdaQueryWrapper<InsProject>(insProject);
        List<InsProject> list = iInsProjectService.list(lqw);
        ExcelUtil<InsProject> util = new ExcelUtil<InsProject>(InsProject.class);
        return util.exportExcel(list, "project");
    }

    /**
     * 获取产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId) {
        return AjaxResult.success(iInsProjectService.getById(projectId));
    }

    /**
     * 新增产品
     */
    @PreAuthorize("@ss.hasPermi('product:project:add')")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsProject insProject) {
        return toAjax(iInsProjectService.save(insProject) ? 1 : 0);
    }

    /**
     * 修改产品
     */
    @PreAuthorize("@ss.hasPermi('product:project:edit')")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsProject insProject) {
        return toAjax(iInsProjectService.updateById(insProject) ? 1 : 0);
    }

    /**
     * 删除产品
     */
    @PreAuthorize("@ss.hasPermi('product:project:remove')")
    @Log(title = "产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds) {
        return toAjax(iInsProjectService.removeByIds(Arrays.asList(projectIds)) ? 1 : 0);
    }
}
